package com.capgemini;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

@Model
public class ChuckNorrisBean {
	
	@Inject ChuckNorrisDao chuckNorrisDao;
	
	public List<ChuckNorrisFact> getFacts() {
		
		Map<Long, ChuckNorrisFact> m = chuckNorrisDao.getFacts();
		
		List<ChuckNorrisFact> l = new ArrayList<ChuckNorrisFact>();
		for (Long f : m.keySet()) {
			l.add(m.get(f));
		}
		
		return l;
	}
	
	private ChuckNorrisFact newFact;

	   @Produces
	   @Named
	   public ChuckNorrisFact getNewFact() {
	      return newFact;
	   }
	   
	   @PostConstruct
	   public void initNewFact() {
		   newFact = new ChuckNorrisFact();
	   }
	   
	   public void addFact() {
		   chuckNorrisDao.addFact(newFact);
		   initNewFact();
	   }
	   
	   public void getRandomFact() {
		   newFact = chuckNorrisDao.getRandomFact();
	   }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((newFact == null) ? 0 : newFact.hashCode());
		return result;
	}
	   
}



